package com.example.vspm.controller;


import com.example.vspm.entity.MeetingPlace;
import com.example.vspm.entity.Person;
import com.example.vspm.entity.TaskForm;
import com.example.vspm.service.MeetingPlaceService;
import com.example.vspm.service.TaskFormService;
import com.example.vspm.util.DateUtil;
import com.example.vspm.util.StateUtil;
import com.example.vspm.util.TimeStampUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

;


/**
 * @author LCL
 * @date 2022/1/6
 * @desc
 */
@RestController
public class PosterController {

    @Autowired
    TaskFormService taskFormService;

    @Autowired
    StateUtil stateUtil;

    @Autowired
    MeetingPlaceService meetingPlaceService;

    @Autowired
    HttpSession request;//通过注解获取一个request,获取session


   ///添加工单
    @RequestMapping("/add")
    public String add(){
        return "taskForm/addTaskForm";
    }

    //创建一开始,数据库中工单状态就更新为草稿,并将增加的工单保存到session中
    //这样做,每次工单插入要保存,工单保存完之后才可插入会场,点击确认保存,然后才能出现点击上传按钮
    @RequestMapping("/addTaskForm")
    public String addTaskForm(TaskForm taskForm, MeetingPlace meetingPlace,Model model, HttpSession session){
        //添加工单
        taskFormService.addTaskForm(taskForm);
        //添加的工单信息存入session,为下面工单上传做个铺垫
        session.setAttribute("addTaskForm",taskForm);
        //添加会场前，更改时间去掉T
        meetingPlace.setArrivalTime(meetingPlace.getArrivalTime().replaceAll("T"," "));
        meetingPlace.setEndTime(meetingPlace.getEndTime().replaceAll("T"," "));
        //添加会场  添加工单的时候,必须同时添加会场,添加会场的taid
        meetingPlace.setTaid(taskForm.getTid());
        meetingPlaceService.addMeetingPlace(meetingPlace);
        //添加工单的pid1
        Person person=(Person) request.getAttribute("person");
        taskFormService.addTaskFormPID1(person.getTid(),taskForm.getTid());
        //更新工单状态为"草稿"
        taskFormService.updateState(stateUtil.taskFormStateSwitch("1"),taskForm.getTid());
         return   "已增加工单";
    }

    //创建工单完成,不上传则不动,自动保存,默认就是草稿
    //上传则更新工单状态为待派单,要在会场填写完成之后执行，代码如下
    @RequestMapping("/updateTaskFormState1")
    public String updateTaskFormState1(Model model){
        TaskForm taskForm1=(TaskForm)request.getAttribute("addTaskForm");
        taskFormService.updateState(stateUtil.taskFormStateSwitch("2"),taskForm1.getTid());
        //"回到工单列表"
        return   "已更新工单";
    }
    //当后续浏览类型为草稿的工单上传 ,草稿的工单一定要有个提交上传的按钮，前端传来tid
    @RequestMapping("/updateTaskFormState11/{tid}")
    public String updateTaskFormState2(@PathVariable("tid") int tid,Model model){
        //更新工单状态
        taskFormService.updateState(stateUtil.taskFormStateSwitch("2"),tid);
        //"回到工单列表"
        return   "已提交草稿";
    }






    /////////////////////////查与自己身份相符的全部工单后,通过前端点击事件,传回某个工单id,以此查看详细的工单信息
//    报单员查看工单
    @GetMapping("/poster/listTaskFormByPid1")
    public List<TaskForm> listTaskFormByPid1(int pid1){
        List<TaskForm> taskForms = taskFormService.listTaskFormByPid1(pid1);
//        return "所有大概的工单";
        return taskForms;
    }




    //根据前端传过来工单ID查看工单详情页,方便显示会场以及接单人员的挑选,评价....
    @GetMapping("/listTaskFormByTid")
    public List<MeetingPlace> listTaskFormByTid(int tid, Model model){
        //查看工单对应的会场
        List<MeetingPlace> meetingPlaces=meetingPlaceService.listMeetingPlaceByTaid(tid);
        model.addAttribute("listMeetingPlaces",meetingPlaces);
        return meetingPlaces;
    }


    ////////////////////////////////删除工单
    @ResponseBody
    @RequestMapping("/deleteTaskForm/{tid}")
    public String deleteTaskForm(@PathVariable("tid") int tid){
        System.out.println(tid);
        taskFormService.deleteTaskForm(tid);
        return "taskForm/listTaskForm";
    }

//    @RequestMapping("/deleteTaskForm")
//    public String deleteTaskForm(@RequestParam("tid")  String tid){
//        System.out.println(tid);
//        int tid1=Integer.parseInt(tid);
//        taskFormService.deleteTaskForm(tid1);
//        return "taskForm/listTaskForm";
//
//    }

}
